package com.websockettest.controller;

import com.websockettest.sse.SseClientUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

@RestController
public class SSEController {
    // 不需要设置client
    // @GetMapping(value = "/sse", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    // public Flux<String> sseTest() {
    //     return Flux.interval(Duration.ofSeconds(1)) // 每秒推送一次数据
    //             .map(sequence -> "Server Time: " + LocalTime.now());
    // }


    @GetMapping("/initSse")
    public SseEmitter createConnect(String userId) {
        return SseClientUtil.createSse(userId);
    }

    /**
     * 关闭连接
     */
    @GetMapping("/closeSse")
    public void closeConnect(String userId){
        SseClientUtil.closeSse(userId);
    }
}
